﻿#include <QTextStream>
#include "CCorrectPointCfg.h"
#include "Public.h"
#include "CSqlOper.h"
#include "CGlobal.h"

CCorrectPointCfg::CCorrectPointCfg(CDevDataBeltRealign* pDevData)
	:m_pDevData(pDevData)
{
	ui.setupUi(this);
	this->setWindowTitle(A2T("跑偏设备配置"));
	m_nType = 0;
	ui.comboBoxIsHaveBeltState->addItem(A2T("否"));
	ui.comboBoxIsHaveBeltState->addItem(A2T("是"));
	ui.comboBoxIsHavePlc->addItem(A2T("否"));
	ui.comboBoxIsHavePlc->addItem(A2T("是"));
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CCorrectPointCfg::slotPushButtonSave);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CCorrectPointCfg::slotPushButtonClose);
	setData();
}

CCorrectPointCfg::CCorrectPointCfg(QString sPointGuid)
{
	ui.setupUi(this);
	this->setWindowTitle(A2T("跑偏设备配置"));
	m_nType = 1;
	m_sPointGuid = sPointGuid;
	ui.comboBoxIsHaveBeltState->addItem(A2T("否"));
	ui.comboBoxIsHaveBeltState->addItem(A2T("是"));
	ui.comboBoxIsHavePlc->addItem(A2T("否"));
	ui.comboBoxIsHavePlc->addItem(A2T("是"));
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CCorrectPointCfg::slotPushButtonSave);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CCorrectPointCfg::slotPushButtonClose);
}

CCorrectPointCfg::~CCorrectPointCfg()
{}

void CCorrectPointCfg::setData()
{
	ui.lineEditPointName->setText(m_pDevData->m_sPointName);
	if (m_pDevData->m_nIsHaveDev == 0)
	{
		ui.comboBoxIsHavePlc->setCurrentText(A2T("否"));
	}
	else if (m_pDevData->m_nIsHaveDev == 1)
	{
		ui.comboBoxIsHavePlc->setCurrentText(A2T("是"));
	}
	ui.lineEditPlcIP->setText(m_pDevData->m_sPlcIpAddress);
	ui.lineEditPlcPort->setText(QString::number(m_pDevData->m_nPlcPort));
	ui.lineEditCamerIP->setText(m_pDevData->m_pDevDataCam->m_sCamerIp);
	ui.lineEditCamerPort->setText(QString::number(m_pDevData->m_pDevDataCam->m_nCamerPort));
	ui.lineEditCamerUser->setText(m_pDevData->m_pDevDataCam->m_sUserName);
	ui.lineEditCamerPassword->setText(m_pDevData->m_pDevDataCam->m_sPassWord);
	ui.lineEditSetValue->setText(QString::number(m_pDevData->m_nRealignSetValue));
	ui.lineEditSetValueTwo->setText(QString::number(m_pDevData->m_nRealignSetValueTwo));
	ui.lineEditAlarmTime->setText(QString::number(m_pDevData->m_nAlarmTime));
	ui.lineEditPicPath->setText(m_pDevData->m_sRecordPath);
	if (m_pDevData->m_nIsHaveBeltRunState == 0)
	{
		ui.comboBoxIsHaveBeltState->setCurrentText(A2T("否"));
	}
	else if (m_pDevData->m_nIsHaveBeltRunState == 1)
	{
		ui.comboBoxIsHaveBeltState->setCurrentText(A2T("是"));
	}
	ui.lineEditLeftRaito->setText(QString::number(m_pDevData->m_dLeftRaito, 'f', 1));
	ui.lineEditRightRaito->setText(QString::number(m_pDevData->m_dRightRaito, 'f', 1));
	ui.lineEditBeltName->setText(m_pDevData->m_sBeltName);
	ui.lineEditRtsp->setText(m_pDevData->m_pDevDataCam->m_sRtspUrlImg);
}

bool CCorrectPointCfg::save()
{
	if (ui.lineEditPointName->text() == "" || ui.lineEditCamerIP->text() == "" || ui.lineEditCamerPort->text() == "" || ui.lineEditCamerUser->text() == "" || ui.lineEditCamerPassword->text() == "")
	{
		messageBox(0, A2T("请填写完整后再进行操作"));
		return false;
	}
	QString sPointName = ui.lineEditPointName->text();
	QString sPlcIp = ui.lineEditPlcIP->text();
	int nPlcPort = ui.lineEditPlcPort->text().toInt();
	QString sCamerIp = ui.lineEditCamerIP->text();
	int nCamerPort = ui.lineEditCamerPort->text().toInt();
	QString sCamerUser = ui.lineEditCamerUser->text();
	QString sPassWd = ui.lineEditCamerPassword->text();
	int nIsHavePlc = 0;
	if (ui.comboBoxIsHavePlc->currentText() == A2T("是"))
	{
		nIsHavePlc = 1;
	}
	else
	{
		nIsHavePlc = 0;
	}
	int nIsHaveBeltState = 0;
	if (ui.comboBoxIsHaveBeltState->currentText() == A2T("是"))
	{
		nIsHaveBeltState = 1;
	}
	else
	{
		nIsHaveBeltState = 0;
	}
	int nSetValue = ui.lineEditSetValue->text().toInt() / 10;
	int nSetValueTwo = ui.lineEditSetValueTwo->text().toInt() / 10;
	if (nSetValue < nSetValueTwo)
	{
		messageBox(1, A2T("一级跑偏判定值应大于二级跑偏判定值"));
		return false;
	}
	int nAlarmTime = ui.lineEditAlarmTime->text().toInt();
	QString sPicPath = ui.lineEditPicPath->text();
	double dLeftRaito = ui.lineEditLeftRaito->text().toDouble();
	double dRightRaito = ui.lineEditRightRaito->text().toDouble();
	QString sBeltName = ui.lineEditBeltName->text();
	QString sRtsp = ui.lineEditRtsp->text();
	QString sErrMsg = "";
	bool bRet = false;
	if (m_nType == 0)
	{
		//如果还需要纠偏设备PLC
		if (nIsHavePlc == 1)
		{
			//首先看下数据库中是否存在通信点表
			QVector<QSqlRecord> vecInfo;
			CSqlOper::instance().selectAllocationTable(m_pDevData->m_sPointGuid, vecInfo, sErrMsg);
			if (vecInfo.size() == 0)
			{
				/*插入modbus地址数据*/
				QList<QStringList> listModbusData;
				bRet = addDefAIModbusData(listModbusData);
				if (false == bRet)
				{
					return false;
				}

				bRet = CSqlOper::instance().addRobModbus(m_pDevData->m_sPointGuid, listModbusData, sErrMsg);
				if (false == bRet)
				{
					messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
					return false;
				}
			}
		}
		else
		{
			CSqlOper::instance().deleteAllocationTable(m_pDevData->m_sPointGuid, sErrMsg);
		}
		bRet = CSqlOper::instance().updateCorrectPoint(m_pDevData->m_sPointGuid, sPointName, sPlcIp, nPlcPort, sCamerIp, nCamerPort, sCamerUser, sPassWd, nIsHavePlc,
			nSetValue, nSetValueTwo, nAlarmTime, sPicPath, nIsHaveBeltState, dLeftRaito, dRightRaito, sBeltName, sRtsp, sErrMsg);
		if (!bRet)
		{
			messageBox(1, A2T("保存失败:%1").arg(sErrMsg));
			return false;
		}
		else
		{
			m_pDevData->m_pMutex->lock();
			m_pDevData->m_sPointName = sPointName;
			m_pDevData->m_sPlcIpAddress = sPlcIp;
			m_pDevData->m_nPlcPort = nPlcPort;
			m_pDevData->m_pDevDataCam->m_sCamerIp = sCamerIp;
			m_pDevData->m_pDevDataCam->m_nCamerPort = nCamerPort;
			m_pDevData->m_pDevDataCam->m_sUserName = sCamerUser;
			m_pDevData->m_pDevDataCam->m_sPassWord = sPassWd;
			m_pDevData->m_nIsHaveDev = nIsHavePlc;
			m_pDevData->m_nRealignSetValue = nSetValue;
			m_pDevData->m_nRealignSetValueTwo = nSetValueTwo;
			m_pDevData->m_nAlarmTime = nAlarmTime;
			m_pDevData->m_pDevDataCam->m_sRecordPath = sPicPath;
			m_pDevData->m_nIsHaveBeltRunState = nIsHaveBeltState;
			m_pDevData->m_dLeftRaito = dLeftRaito;
			m_pDevData->m_dRightRaito = dRightRaito;
			m_pDevData->m_sBeltName = sBeltName;
			m_pDevData->m_pDevDataCam->m_sRtspUrlImg = sRtsp;
			m_pDevData->m_pMutex->unlock();
			messageBox(0, A2T("保存成功"));
			return true;
		}
	}
	else if (m_nType == 1)//添加
	{
		//如果还需要纠偏设备PLC
		if (nIsHavePlc == 1)
		{
			//首先看下数据库中是否存在通信点表
			QVector<QSqlRecord> vecInfo;
			CSqlOper::instance().selectAllocationTable(m_sPointGuid, vecInfo, sErrMsg);
			if (vecInfo.size() == 0)
			{
				/*插入modbus地址数据*/
				QList<QStringList> listModbusData;
				bRet = addDefAIModbusData(listModbusData);
				if (false == bRet)
				{
					return false;
				}

				bRet = CSqlOper::instance().addRobModbus(m_sPointGuid, listModbusData, sErrMsg);
				if (false == bRet)
				{
					messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
					return false;
				}
			}
		}
		else
		{
			CSqlOper::instance().deleteAllocationTable(m_sPointGuid, sErrMsg);
		}
		bRet = CSqlOper::instance().insertCorrectPoint(m_sPointGuid, sPointName, sPlcIp, nPlcPort, sCamerIp, nCamerPort, sCamerUser, sPassWd, nIsHavePlc, nSetValue,
			nSetValueTwo, nAlarmTime, sPicPath, nIsHaveBeltState, dLeftRaito, dRightRaito, sBeltName, sRtsp, sErrMsg);
		if (!bRet)
		{
			messageBox(1, A2T("保存失败:%1").arg(sErrMsg));
			return false;
		}
		else
		{
			messageBox(0, A2T("保存成功"));
			return true;
		}
	}
}

bool CCorrectPointCfg::addDefAIModbusData(QList<QStringList>& listModbusData)
{
	QString sPath = QCoreApplication::applicationDirPath();
	sPath.append("/DLL/CorrectCfg/beltRealignModbusCfg.csv");

	QFile file(sPath);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		messageBox(2, A2T("读取默认Modbus数据失败!"));
		return false;
	}

	QTextStream stream(&file);
	int nIndex = 0;
	while (!stream.atEnd())
	{
		QString line = stream.readLine();
		QStringList row = line.split(","); // 使用逗号分隔数据
		listModbusData.append(row);
	}

	file.close();

	//移除第1个标题
	listModbusData.removeAt(0);

	return true;
}

void CCorrectPointCfg::refreshData(bool bRefresh)
{

}

void CCorrectPointCfg::slotPushButtonSave()
{
	save();
}

void CCorrectPointCfg::slotPushButtonClose()
{
	close();
}
